package com.bezdelnick.forum.web.resource;

import org.apache.wicket.markup.html.image.resource.BufferedDynamicImageResource;
import org.apache.wicket.request.resource.IResource;
import org.apache.wicket.request.resource.ResourceReference;
import org.apache.wicket.util.file.File;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: Bezdelnick
 * Date: 04.09.12
 * Time: 14:48
 * To change this template use File | Settings | File Templates.
 */
public class ImageFromFileSystemReference extends ResourceReference {

    private String path;

    public ImageFromFileSystemReference(String name, String imagePathAndName) {
        super(name);
        this.path = imagePathAndName;
    }

    @Override
    public IResource getResource() {
        final BufferedDynamicImageResource resource = new BufferedDynamicImageResource();
        final BufferedImage image;
        try {
            image = ImageIO.read(new File(this.path));
            resource.setImage(image);
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return resource;
    }
}
